<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-table :data="rolenames" stripe style="width: 100%" border>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="tag in scope.row.children"
              :key="tag.id"
              :gutter="20"
              :class="['bdbuttom', 'elRowAlign']"
            >
              <!-- 一级权限展开列 -->
              <el-col :span="5" type="flex" class="elCol1">
                <div>
                  <el-tag
                    closable
                    @close="removePermissions(scope.row, tag.id)"
                  >
                    <span>
                      {{ tag.authName }}
                    </span>
                  </el-tag>
                  <i class="el-icon-caret-right"></i>
                </div>
              </el-col>
              <!-- 二三级权限展开列 -->
              <el-col :span="19">
                <el-row
                  v-for="tagMiddle in tag.children"
                  :key="tagMiddle.id"
                  :class="['bdbuttom', 'elRowAlign']"
                >
                  <!-- 二级权限 -->
                  <el-col :span="6">
                    <el-tag
                      closable
                      type="success"
                      @close="removePermissions(scope.row, tagMiddle.id)"
                    >
                      <span>
                        {{ tagMiddle.authName }}
                      </span>
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限 -->
                  <el-col :span="18">
                    <el-tag
                      closable
                      type="warning"
                      v-for="tagRight in tagMiddle.children"
                      :key="tagRight.id"
                      @close="removePermissions(scope.row, tagRight.id)"
                    >
                      <span>
                        {{ tagRight.authName }}
                      </span>
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
              <!-- 右侧展开列 -->
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scopeIndex">
            <el-button type="primary" icon="el-icon-edit"></el-button>
            <el-button type="primary" icon="el-icon-delete"></el-button>
            <el-button
              type="primary"
              icon="el-icon-share"
              @click="setruns(scopeIndex.$index)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="setRightDialogVisible"
      width="30%"
      center
    >
      <el-tree
        :data="rolenamesTree"
        :props="rolenamesTreeProps"
        show-checkbox
        default-expand-all
        check-on-click-node
        node-key="id"
        ref="nodesOfAddedChildAssocs"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            setRightDialogVisible = false;
            addUsedPermissions();
          "
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolenames: [],
      setRightDialogVisible: false,
      rolenamesTree: [],
      rolenamesTreeProps: {
        children: "children",
        label: "authName",
      },
      setKeysRolenames: [],
      UsedPermissionsId: 0,
    };
  },
  methods: {
    // 获取权限列表
    async setRolesToBroadcastPresence() {
      const { data: res } = await this.$http({
        url: "/roles",
        method: "get",
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.rolenames = res.data;
    },
    // 删除单个权限
    async removePermissions(roles, rigthID, da5) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") return this.$message.info("已取消删除");
      const { data: res } = await this.$http({
        url: `roles/${roles.id}/rights/${rigthID}`,
        method: "delete",
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      roles.children = res.data;

      // this.setRolesToBroadcastPresence();
      this.$message.success(res.meta.msg);
    },
    // 添加权限弹框
    async setruns(rowId) {
      const { data: res } = await this.$http({
        url: "/rights/tree",
        method: "get",
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.setKeysRolenames = [];
      this.setKeysRolenamesComputed(this.rolenames[rowId].children);
      this.setRightDialogVisible = true;
      this.rolenamesTree = res.data;
      this.UsedPermissionsId = this.rolenames[rowId].id;
      // 将实际拥有权限预渲染给树形结构
      this.$nextTick(function() {
        this.$refs.nodesOfAddedChildAssocs.setCheckedNodes(
          this.setKeysRolenames
        );
      });
    },
    // 递归计算实际权限
    setKeysRolenamesComputed(SetClientData) {
      SetClientData.forEach((item) => {
        if (item.children) {
          this.setKeysRolenamesComputed(item.children);
        } else {
          let obj = {};
          obj.id = item.id;
          obj.label = item.authName;
          this.setKeysRolenames.push(obj);
        }
      });
    },
    // 添加权限
    async addUsedPermissions() {
      // 获取半选中状态的多选框
      let UsedPermissions1 = this.$refs.nodesOfAddedChildAssocs.getHalfCheckedKeys();
      // 获取选中状态的多选框
      let UsedPermissions2 = this.$refs.nodesOfAddedChildAssocs.getCheckedKeys();
      // 合并数组
      UsedPermissions1.push(UsedPermissions2);
      // 转换字符串
      UsedPermissions1 = UsedPermissions1.join(",");
      const { data: res } = await this.$http({
        method: "post",
        url: `roles/${this.UsedPermissionsId}/rights`,
        data: {
          rids: UsedPermissions1,
        },
      });
      if (res.meta.status !== 200)
        return this.$message.error("添加失败，请重试");
      this.setRolesToBroadcastPresence();
      this.$message.success(res.meta.msg);
    },
  },
  created() {
    // 预渲染权限列表
    this.setRolesToBroadcastPresence();
  },
};
</script>

<style lang="less" scoped>
.bdtop {
  border-top: 1px solid #eee;
}
.bdbuttom {
  border-bottom: 1px solid #eee;
}
.elRowAlign {
  display: flex;
  align-items: center;
}
.el-tag {
  margin: 7px;
}
.elCol1 {
  display: flex;
  justify-content: center !important;
}
</style>
